//
//  WelcomeViewController.swift
//  01-WeiBo
//
//  Created by 李晓东 on 17/7/4.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

import UIKit
import SnapKit

class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backgroundImageView
        setUI()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        t()
    }

    var backgroundImageView = UIImageView(image: UIImage(named:"ad_background"))
    fileprivate var iconView = UIImageView(image: UIImage(named: "iconImg"))
    fileprivate var welcomeView = UILabel(labelTxt: "欢迎回来", font:  UIFont.systemFont(ofSize: 14), textColor: UIColor.darkGray)
}

extension WelcomeViewController{
    
    func setUI() {
        welcomeView.sizeToFit()
        view.addSubview(iconView)
        view.addSubview(welcomeView)
        welcomeView.alpha = 0
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        
        
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.bottom.equalTo(view.snp.bottom).offset(-360)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        welcomeView.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(view.snp.top).offset(240)
        }
        
    }
    
    func t() {
        iconView.snp.updateConstraints { (make) in
            make.bottom.equalTo(view.snp.bottom).offset(-view.frame.height + 200)
        }
        UIView.animate(withDuration: 1.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: {
            self.view.layoutIfNeeded()
        }) { (result) in
            UIView.animate(withDuration: 0.8, animations: { 
                self.welcomeView.alpha = 1
            }, completion: { (_) in
//                self.present(MainViewController(), animated: false, completion: {
//                    
//                })
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBSwitchRootViewControllerNotification), object: nil)
            })
            
            
        }
    }
}
